# Setup environment ----
library(parallel)
library(data.table)
library(bigKRLS)
source("R/functions.R")
results_path <- "results/replication/"
load(paste0(results_path, "house_analysis_data.RData"))
load(paste0(results_path, "senate_analysis_data.RData"))

# House KRLS w/ lagged outcome ----
house_krls_model_lag <- bigKRLS::bigKRLS(X = hou_X_lag,
  y = hou_y_lag, noisy = TRUE)
save.bigKRLS(house_krls_model_lag,
  model_subfolder_name = paste0(results_path, "house_krls_model_lag"))

# Senate KRLS w/ lagged outcome ----
senate_krls_model_lag <- bigKRLS::bigKRLS(X = sen_X_lag,
  y = sen_y_lag, noisy = TRUE)
save.bigKRLS(senate_krls_model_lag,
  model_subfolder_name = paste0(results_path, "senate_krls_model_lag"))

# House KRLS w/ lagged outcome + lagged spending ----
hou_X_lag2 <- cbind(hou_X_lag, data.matrix(house_data_lag[, lag_DSA]))
colnames(hou_X_lag2)[ncol(hou_X_lag2)] <- "lag_DSA"
ok <- complete.cases(hou_X_lag2)
hou_X_lag2 <- hou_X_lag2[ok, ]
hou_y_lag2 <- hou_y_lag[ok, , drop = FALSE]
house_krls_model_lag2 <- bigKRLS::bigKRLS(X = hou_X_lag2,
  y = hou_y_lag2, noisy = TRUE)
save.bigKRLS(house_krls_model_lag2,
  model_subfolder_name = paste0(results_path, "house_krls_model_lag2"))

# Senate KRLS w/ lagged outcome + lagged spending ----
sen_X_lag2 <- cbind(sen_X_lag, data.matrix(senate_data_lag[, lag_DSA]))
colnames(sen_X_lag2)[ncol(sen_X_lag2)] <- "lag_DSA"
ok <- complete.cases(sen_X_lag2)
sen_X_lag2 <- sen_X_lag2[ok, ]
sen_y_lag2 <- sen_y_lag[ok, , drop = FALSE]
senate_krls_model_lag2 <- bigKRLS::bigKRLS(X = sen_X_lag2,
  y = sen_y_lag2, noisy = TRUE)
save.bigKRLS(senate_krls_model_lag2,
  model_subfolder_name = paste0(results_path, "senate_krls_model_lag2"))
